/******************************************************************/
/*****************auto set tab index to form **********************/
/******************************************************************/
var objFirstFocus='';
var objLastFocus='';
function reset_tabindex(formObject) {
    for (var i = 0; i < formObject.elements.length; i++) {
        reset_tabindexByID(formObject.elements[i]);
    }
    //-- image-focusable feature
    //reset_tabindexForImage();
}
function keepFirstLastTab(item)
{
	if(objFirstFocus == '')
	{
		if(item.type == 'text')
		{
			objFirstFocus = item.name;
			objLastFocus = item.name;
		}
	}else
	{
		//alert(item.name);
		//alert(item.disabled);
		//alert(item.readOnly);
		objLastFocus = item.name;
	}
}
function reset_tabindexByID(item) {
    if (typeof(item) != 'undefined') {
        if (item.type == 'select-one') {
            if (!item.disabled) {
                item.tabIndex = "0";
                keepFirstLastTab(item);
            } else {
                item.tabIndex = "-1";
            }
        } else if(item.type == 'checkbox')
        {
        	item.tabIndex = "-1";
        }else {
            if (typeof(item.disabled) != 'undefined') {
                if (item.disabled) {
                    item.tabIndex = "-1";
                } else {
                    item.tabIndex = "0";
                    keepFirstLastTab(item);
                    //register select javascript
                    if(item.type == 'text' || item.type == 'textarea')
                    {
                    	item.onfocus =  function() {this.select();}
                    }
                }
            }
            if (typeof(item.readOnly) != 'undefined') {
            	if(item.type != 'submit' && item.type != 'button')  //button does not have read-only property
            	{
	                if (item.readOnly) {
	                    item.tabIndex = "-1";
	                } else {
	                    item.tabIndex = "0";
	                    keepFirstLastTab(item);
	                    //register select javascript
	                    if(item.type == 'text' || item.type == 'textarea')
	                    {
	                    	item.onfocus =  function() {this.select();}
	                    }
	                }
	            }
            }
        }

        mandatoryObj = document.getElementById("mandatory" + item.name);
        if (mandatoryObj)
        {
            if (item.tabIndex == "-1")
            {
                mandatoryObj.style.display = "none";
            }
            else
            {
                mandatoryObj.style.display = "";
            }
        }

    }
}

function reset_tabindexForImage() {
    //-- image-focusable feature
    var imgBtns = document.getElementsByTagName("img");
    if (imgBtns != "undefined") {
        var len = imgBtns.length;
        for (var i = 0; i < len; i++)
        {
            var imgBtn = imgBtns[i];
            var id = imgBtn.id;
            if (id == 'undefined')id = "";
            if (id.indexOf('btn') > -1) {
                //alert(imgBtn.id+' , '+imgBtn.disabled);
                if (typeof(imgBtn.disabled) != 'undefined') {
                    if (!imgBtn.disabled) {
                        imgBtn.tabIndex = "0";
                        imgBtn.onkeydown = img_Onkeydown;
                    }
                }
            }
        }
    }
}

function img_Onkeydown() {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    var item = e.srcElement;
    if (code == 13) {
        item.onclick();
    }
}


function turnOffAutoComlete(formObject) {
    try {
        formObject.autocomplete = "off";
    } catch(e) {
    }
}

function registerAutoToolTip() {
    try {
        var inputs = document.getElementsByTagName("input");
        if (inputs != "undefined") {
            var len2 = inputs.length;
            for (var i = 0; i < len2; i++)
            {
                var input = inputs[i];
                var id = input.id;
                if (id == 'undefined')id = "";
                if (input.type && input.type == 'text') {
                    if ( input.className == "textboxchardisplay" //|| 
						 //input.className == "textboxchardisplaynonitalictooltip" ||                     
                         //input.className == "textboxchartooltip" || 
                         //input.className == "textboxchartooltipdisable"
                        ) {
                        input.onmouseover = function() {
                            try {
                            ddrivetip(this.value);
                            } catch(e){}
                        };
                        input.onmouseout = function() {
                            try {
                            hideddrivetip();
                            }catch(e) {}
                        };
                    }
                }
            }
        }
    } catch(e) {
    }
}

// FOCUS
function focusFirstInputField(form)
{
	var len = form.elements.length;
    for (var i = 0; i < len; i=i+1) 
    {
         var item = form.elements[i];
         if(typeof(item) != 'undefined')
         {
         	// every type that can input
         	if(item.type == 'text')
         	{
         		if (!item.readOnly && !item.disabled && item.type != 'hidden') {
	         		item.focus();
	         		return false;
         		}
         	}
         }
    }
}

/******************************************************************/
/********************** other utils script. ***********************/
/******************************************************************/
//Trim whitespace from left and right sides of s.
function trim(s) {
    return s.replace(/^\s*/, "").replace(/\s*$/, "");
}

/*
format string if key match [qty, amt, price, account_no, account_id, txndate]
key : name of value to format
value : value to format
return : formatedStrValue 
*/
function format(key, value){
    if(key.toLowerCase().indexOf('qty') >= 0){
    	value = formatDecimal( value, 0, true, false, BigDecimal.prototype.ROUND_DOWN );
    }else if(key.toLowerCase().indexOf('amt') >= 0){
    	value = formatDecimal( value, 2, true, false, BigDecimal.prototype.ROUND_DOWN );
    }else if(key.toLowerCase().indexOf('par_price') >= 0){
    	value = formatDecimal( value, 10, true, false, BigDecimal.prototype.ROUND_DOWN );
    }else if(key.toLowerCase().indexOf('price') >= 0){
    	value = formatDecimal( value, 2, true, false, BigDecimal.prototype.ROUND_DOWN );
    }else if(key.toLowerCase().indexOf('account_id') >= 0){
    	value = formatAccountID(value, false);
    }else if(key.toLowerCase().indexOf('txndate') >= 0) {
        value = formatDate(value);
    }
    return value;
}

/**
 * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
 * http://javascript.internet.com
 */
function isValidEmail(emailStr) {
    if (emailStr.length == 0) {
        return true;
    }
    var emailPat = /^(.+)@(.+)$/;
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars = "\[^\\s" + specialChars + "\]";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = /^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
    var atom = validChars + '+';
    var word = "(" + atom + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
        return false;
    }
    var user = matchArray[1];
    var domain = matchArray[2];
    if (user.match(userPat) == null) {
        return false;
    }
    var IPArray = domain.match(ipDomainPat);
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                return false;
            }
        }
        return true;
    }
    var domainArray = domain.match(domainPat);
    if (domainArray == null) {
        return false;
    }
    var atomPat = new RegExp(atom, "g");
    var domArr = domain.match(atomPat);
    var len = domArr.length;
    if ((domArr[domArr.length - 1].length < 2) ||
        (domArr[domArr.length - 1].length > 3)) {
        return false;
    }
    if (len < 2) {
        return false;
    }
    return true;
}

/******************************************************************/
/***************** Trim with specific symbol **********************/
/******************************************************************/
function trimSymbol(Str,Symbol) {
	var trimStr = "";
	for(var i=0; i<Str.length; i++) {
		if(Str.substring(i,i+1)!=Symbol) {
			trimStr = trimStr + Str.substring(i,i+1);
		}
	}
	return trimStr;
}